﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Classes
{

    public class Booking
    {
        //Connection connetionObj;
        IList<Reservation> list;
        
        public Booking() { 

            list = new List<Reservation>();
                         
        }
        

        //checks if there is enough seats left on all flights in connection
        private bool Register(Connection connection, int seatsWanted, string fname, string lname,string address,string country,string phone,string email,string passport) {

          bool success = false;

           foreach (Flight item in connection.FlightList())
	        {
                
                if ((item.NoOfSeats - seatsWanted) >= 0) //if there are less seats than wanted, abort booking.
                {
                    
                    if ((item.NoOfSeats - seatsWanted) == 0)
                    {
                        //remove flight from list but continue this booking and kill all other running bookings that involves this flight
                    }
                     item.NoOfSeats = item.NoOfSeats - seatsWanted; //to decrease number of available seats
                    // for(int i = 0; i < seatsWanted; i++)
                   // { 
                            // seatNO = seatsAvailable + i; or something like that to specifie seats numbers if needed, I don't enable this because not sure if needed
                   // }
                     
                    list.Add(new Reservation(item, connection, seatsWanted, fname, lname, address, country, phone, email, passport)); // all basic info comes from GUI so for now we'll use presets
                    success = true;
                }
                else { 
                //there is not enough seats left
                    //inform customers ?
                        //abort booking
                   
                    cancelReservations();
                    success = false;
                    //break;
                }
	        } 
            return success;
        }

        private void cancelReservations() { 
        
            list.Clear();
        
        }
        //books tickets
        public IList<Reservation> book(Connection connection, int seatsWanted, string fname, string lname,string address,string country,string phone,string email,string passport)
        {
            if (Register(connection, seatsWanted, fname, lname, address, country, phone, email, passport)) //performs booking and if reservation list if filled correctly gives true, othervise aborts booking
            {
                Console.WriteLine("Booking");
                return list;
            }
            else {
                Console.WriteLine("Canceled");
             return null;
            }

            
        }
    }
}
